"""
    顺序栈 sequence_stack
    用数组实现的栈称之为顺序栈
"""

from 栈 import Stack


class SequenceStack(Stack):
    """顺序栈"""

    def __init__(self):
        super().__init__()
        self._items = []

    def append(self, item):
        self._items.append(item)
        print("添加元素成功")
        print(self.get_stack)

    def pop(self):
        if self.is_empty:
            print("空栈无元素可pop")
            return
        self._items.pop()
        print("删除元素成功")
        print(self.get_stack)

    @property
    def is_empty(self):
        return self._items == []

    @property
    def top(self):
        return self._items[-1]

    @property
    def get_stack(self):
        return self._items


class DoubleStack(Stack):
    def __init__(self, length):
        super().__init__()
        self._items = [0 for _ in range(length)]
        self.head = -1
        self.tail = len(self._items)

    def is_empty(self):
        if self.head == -1:
            print("头栈为空")
            return True
        elif self.tail == len(self._items):
            print("尾栈为空")
            return True
        else:
            return False

    @property
    def is_full(self):
        if self.head + 1 == self.tail:
            return True
        return False

    def append(self, stack, data):
        if self.is_full:
            print("栈已满，无法继续添加元素")
            return False
        if stack == "head":
            self.head += 1
            self._items[self.head] = data
            print("头栈添加元素成功")
        else:
            self.tail -= 1
            self._items[self.tail] = data
            print("尾栈添加元素成功")
        print(self.get_stack())
        return True

    def pop(self, stack):
        if stack == "head":
            if self.head == -1:
                print("头栈为空,无法继续出栈")
                return False
            self._items[self.head] = 0
            self.head -= 1
            print("删除头栈元素成功")

        if stack == "tail":
            if self.tail == len(self._items):
                print("尾栈为空,无法继续出栈")
                return False
            self._items[self.tail] = 0
            self.tail += 1
            print("删除尾栈元素成功")
        print(self.get_stack())
        return True

    def get_stack(self):
        print(self._items)


if __name__ == "__main__":
    # 顺序栈
    seq_stack = SequenceStack()
    seq_stack.append(1)
    seq_stack.append(4)
    seq_stack.append(5)
    seq_stack.pop()
    seq_stack.append(6)
    seq_stack.pop()
    seq_stack.pop()
    seq_stack.pop()
    seq_stack.pop()

    # 共享栈
    double_stack = DoubleStack(length=20)
    double_stack.append("head", 2)
    double_stack.append("head", 4)
    double_stack.append("head", 6)
    double_stack.append("head", 8)
    double_stack.pop("head")
    double_stack.append("tail", 2)
    double_stack.pop("tail")
    double_stack.pop("tail")
    for i in range(20):
        double_stack.append("tail", i + 1)
